package com.settlement.system.service.impl;

import com.settlement.system.model.entity.BsOmsReturnOrderDetail;
import com.settlement.system.mapper.BsOmsReturnOrderDetailMapper;
import com.settlement.system.service.BsOmsReturnOrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.BsOmsReturnOrderDetailQuery;
import com.settlement.system.model.form.BsOmsReturnOrderDetailForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.BsOmsReturnOrderDetailImportVo;

/**
 * <p>
 * 百盛退货明细表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-09-06
 */
@Service
public class BsOmsReturnOrderDetailServiceImpl extends ServiceImpl<BsOmsReturnOrderDetailMapper, BsOmsReturnOrderDetail> implements BsOmsReturnOrderDetailService {

    @Override
    public Page<BsOmsReturnOrderDetail> getPage(BsOmsReturnOrderDetailQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<BsOmsReturnOrderDetail> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveBsOmsReturnOrderDetail(BsOmsReturnOrderDetailForm bsOmsReturnOrderDetailForm) {
         BsOmsReturnOrderDetail bsOmsReturnOrderDetail = new BsOmsReturnOrderDetail();
         BeanUtils.copyProperties(bsOmsReturnOrderDetailForm, bsOmsReturnOrderDetail);
         bsOmsReturnOrderDetail.setCreateTime(null);
         return this.saveOrUpdate(bsOmsReturnOrderDetail);
     }

     @Override
     public BsOmsReturnOrderDetailForm getBsOmsReturnOrderDetailFormData(Long id) {
         BsOmsReturnOrderDetail bsOmsReturnOrderDetail = this.getById(id);
         BsOmsReturnOrderDetailForm bsOmsReturnOrderDetailForm = new BsOmsReturnOrderDetailForm();
         BeanUtils.copyProperties(bsOmsReturnOrderDetail, bsOmsReturnOrderDetailForm);
         return bsOmsReturnOrderDetailForm;
     }

     @Override
     public boolean deleteBsOmsReturnOrderDetail(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<BsOmsReturnOrderDetailImportVo> voList)  {
        List<BsOmsReturnOrderDetail> bsOmsReturnOrderDetailList = new ArrayList<>();
        for (BsOmsReturnOrderDetailImportVo importVo : voList) {
            BsOmsReturnOrderDetail bsOmsReturnOrderDetail = new BsOmsReturnOrderDetail();
            BeanUtils.copyProperties(importVo, bsOmsReturnOrderDetail);
            bsOmsReturnOrderDetailList.add(bsOmsReturnOrderDetail);
        }
        this.saveOrUpdateBatch(bsOmsReturnOrderDetailList);
    }

    @Override
    public List<BsOmsReturnOrderDetailImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<BsOmsReturnOrderDetail> bsOmsReturnOrderDetailList = this.getBaseMapper().selectBatchIds(ids);
        List<BsOmsReturnOrderDetailImportVo> voList = new ArrayList<>();
        for (BsOmsReturnOrderDetail bsOmsReturnOrderDetail : bsOmsReturnOrderDetailList) {
            BsOmsReturnOrderDetailImportVo vo = new BsOmsReturnOrderDetailImportVo();
            BeanUtils.copyProperties(bsOmsReturnOrderDetail, vo);
            voList.add(vo);
        }
        return voList;
    }
}
